package cur.nature.component.quartz.config;

import cur.nature.tinyorm.core.template.DefaultJdbcTemplate;
import cur.nature.framework.core.log.LOG;
import cur.nature.framework.core.util.StringUtil;
import jakarta.annotation.Resource;
import org.quartz.Calendar;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;

/**
 * @author wujian
 * @date 2021/08/28
 */
@Configuration
@DependsOn(DefaultJdbcTemplate.DATASOURCE)
public class QuartzConfiguration extends QuartzAutoConfiguration {

    @Autowired(required = false)
    @Qualifier(CompQuartzDBConfig.DATASOURCE)
    private DataSource curQuartzDataSource;

    @Value("${cur.component.quartz.enable:false}")
    private boolean isEnable;

    @Value("${spring.application.name}")
    private String appName;

    @Autowired(required = false)
    private QuartzJobListener quartzJobListener;

    @Autowired(required = false)
    private QuartzTriggerListener quartzTriggerListener;

    @Bean(name = "schedulerFactoryBean")
    public SchedulerFactoryBean quartzScheduler(QuartzProperties properties, ObjectProvider<SchedulerFactoryBeanCustomizer> customizers,
                                                ObjectProvider<JobDetail> jobDetails, Map<String, Calendar> calendars,
                                                ObjectProvider<Trigger> triggers, ApplicationContext applicationContext) {
        SchedulerFactoryBean schedulerFactoryBean = super.quartzScheduler(properties, customizers, jobDetails, calendars, triggers, applicationContext);
        LOG.info("任务调度是否启动: " + isEnable + ", appName:" + appName);
        if(isEnable){
            schedulerFactoryBean.setDataSource(curQuartzDataSource);
//            schedulerFactoryBean.setAutoStartup(false);
//            schedulerFactoryBean.setJobFactory(myJobFactory);
            SpringBeanJobFactory springBeanJobFactory = new SpringBeanJobFactory();
            schedulerFactoryBean.setJobFactory(springBeanJobFactory);
            schedulerFactoryBean.setGlobalJobListeners(quartzJobListener);
            schedulerFactoryBean.setGlobalTriggerListeners(quartzTriggerListener);
            //默认设置为微服务的名称
            if(StringUtil.isNotEmpty(appName)){
                properties.getProperties().put("org.quartz.scheduler.instanceName",appName);
            }
            schedulerFactoryBean.setQuartzProperties(this.asProperties(properties.getProperties()));
        }else{
            schedulerFactoryBean.setQuartzProperties(null);
            schedulerFactoryBean.setAutoStartup(false);
        }
        //获取配置属性
//        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
//        propertiesFactoryBean.setLocation(new ClassPathResource("quartz.properties"));
//        //在quartz.properties中的属性被读取并注入后再初始化对象
//        propertiesFactoryBean.afterPropertiesSet();
//        //创建SchedulerFactoryBean
//        Properties pro = propertiesFactoryBean.getObject();
//        SchedulerFactoryBean factory = new SchedulerFactoryBean();
//        factory.setOverwriteExistingJobs(true);
//        factory.setAutoStartup(true);
////        factory.setQuartzProperties(pro);
//        factory.setQuartzProperties(this.asProperties(properties.getProperties()));
//        factory.setJobFactory(myJobFactory);

        return schedulerFactoryBean;
    }

    private Properties asProperties(Map<String, String> source) {
        Properties properties = new Properties();
        properties.putAll(source);
        return properties;
    }

}
